#!/bin/bash
# Copyright © 2023-2024 Jeffrey Cliff
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
 

TEMP=`mktemp`
TEMP1=`mktemp`
BASEDIR=/home/themusicgod1
RED="\x1343"

MAXLEVEL=`grep -i "maximum" $BASEDIR/tools/grabLaundry.sh |  awk -F' ' '{print $8}' | awk -F')' '{print $1}'`".0"
# MAXLEVEL="156.0"
echo "maxlevel" $MAXLEVEL
# MAXLEVEL="134.0"
WHAT=$1
echo $WHAT

TEST_FOR_THREADS=$1
THREADWHAT=`echo $TEST_FOR_THREADS | grep -i "www.threads.net"`
if [ ! -z $THREADWHAT ] ; then
	setterm --foreground "red"
	echo "old threads" $WHAT
	WHAT2=`echo $WHAT | sed -e "s/www.threads.net/threads.com/g"`
	echo "what2 " $WHAT2
	echo "update news set url = \""$WHAT2"\" where url = \""$WHAT"\";"
	setterm --foreground "default"
	exit;
fi
THREADWHAT=`echo $TEST_FOR_THREADS | grep -i "threads.net"`
if [ ! -z $THREADWHAT ] ; then
	setterm --foreground "red"
	echo "old threads" $WHAT
	WHAT2=`echo $WHAT | sed -e "s/threads.net/threads.com/g"`
	echo "what2 " $WHAT2
	echo "update news set url = \""$WHAT2"\" where url = \""$WHAT"\";"
	setterm --foreground "default"
	exit;
fi


WHAT=`echo $WHAT | sed -e "s/www.threads.net/threads.net/"`
echo "what:" $WHAT
# https://www.cyberciti.biz/faq/linux-unix-shell-programming-converting-lowercase-uppercase/
WHAT_LOWER=`echo $WHAT | tr '[:upper:]' '[:lower:]'`
# we ALWAYS want to be dealing with lowercase
WHAT=$WHAT_LOWER
if [ -z $WHAT ] ; then
	echo "you need to give this script some input"
	exit;
fi
echo "what:" $WHAT
setterm --foreground "red"
check_user_for_caps.sh $WHAT
setterm --foreground "default"
DB=/home/themusicgod1/TDL/urls.db
FEDIDB=/home/themusicgod1/TDL/fedi.db

#the problem here is that like 


LEVEL=`sqlite3 $DB -line "select level from news where url = \""$WHAT"\"" | grep -i 'level' | awk -F' ' '{print $3}'`

if [[ -z $LEVEL ]]; then
	echo "zero - we have a new user"
 	NEWID=`sqlite3 $DB -line "select max(id) from news" | grep -i 'id' | awk -F' ' '{print $3}'`
#	echo $NEWID
	NEWID2=`echo $NEWID "+ 1" | bc`
#	echo $NEWID2
	DOMAIN=`echo $WHAT | awk -F'@' '{print $3}'`;
#	NEWLEVEL=`grep  -i $DOMAIN $BASEDIR/FediSus.html | head -n 1 | awk -F' ' '{print $2}'`
#	echo $NEWLEVEL

	# echo "we need to check if it's threads here";

        if [[ -z $DOMAIN ]]; then 
	echo "something went wrong here : "  $DOMAIN  "should be a domain"
	echo "what is " $WHAT
	echo "this should give results:"
	echo $WHAT | grep -i '@.*@.*'
	if [ $? -eq 1 ]; then 
		echo "you probably are missing a @ in your fedi id"
	fi
	exit
	fi

	if [ $DOMAIN = "www.threads.net" ]; then
	echo "its threads";
	DOMAIN="threads.net"
	echo "NEWID2" $WHAT
	WHAT=`echo $WHAT | sed -e "s/www.threads.net/threads.net/"`
	echo "NEWID2" $WHAT
	else
	echo "its' not threads";
	fi

	NEWLEVEL=`sqlite3 $FEDIDB -line "select level from instances where name like '"$DOMAIN"';" | grep -i 'level' | awk -F' ' '{print $3}'`
	HOWMANY=`how_many_arguments.sh $NEWLEVEL`
        if [[ $HOWMANY -gt 1 ]]; then
	    setterm --foreground red
            echo "looks like we have too many levels: " $WHAT
	    setterm --foreground default
	    exit 1
        fi
	if [[ -z $NEWLEVEL ]]; then
		echo "insert into news values ("$NEWID2",\""$WHAT"\",0,'','','');"
	else
		echo "insert into news values ("$NEWID2",\""$WHAT"\","$NEWLEVEL",'','','');"
	fi
	exit
fi

ID=`sqlite3 $DB -line "select id from news where url like \""$WHAT"\"" | grep -i 'id' | awk -F' ' '{print $3}'`
DOMAIN=`echo $1 | awk -F'@' '{print $3}'`
if [ $DOMAIN = "www.threads.net" ]; then
	echo "its threads";
	DOMAIN="threads.net"
	else
	echo "its' not threads";
fi
echo "domain is " $DOMAIN

# deal with the great cloudwall
ISCLOUDWALL=`sqlite3 $FEDIDB -line "select iscf from instances where name = \"$DOMAIN\"" | sed -e "s/ iscf = //" `
#echo sqlite3 $FEDIDB -line "select iscf from instances where name = \"$DOMAIN\""  
#echo "db" $FEDIDB
#echo "cloudwall" $ISCLOUDWALL
# thanks https://dev.to/dak425/how-to-check-if-a-string-is-empty-in-bash-264l
ICWA=`how_many_arguments.sh $ISCLOUDWALL`

if [[ $ICWA -gt 1 ]]; then
    setterm --foreground red
    echo "something up with " $DOMAIN
    setterm --foreground default
fi

echo "is cloudwall" $ISCLOUDWALL
if [[ -z $ISCLOUDWALL  ]] ; then
    setterm --foreground red
    echo "we are probably missing " $DOMAIN " in our database"
    echo "blocks:" `how_many_blocks_estimate.sh $DOMAIN`
#    echo "cloudflare?"  `is_cloudflare $DOMAIN`
setterm --foreground "yellow"
echo "is_cloudflare is broken"
setterm --foreground "default"
    echo $DOMAIN >> /tmp/totallymissingdomains
    setterm --foreground default
    exit;
fi
if [ $ISCLOUDWALL -eq 2 ] ; then
    echo $DOMAIN " was in the database but cf status not known"
    setterm --foreground yellow
    echo "CLOUDFLARE - STATUS UNKNOWN"
setterm --foreground "yellow"
echo "is_cloudflare is broken"
setterm --foreground "default"
#    echo "cloudflare?"  `is_cloudflare $DOMAIN`
    echo $DOMAIN >> /tmp/missingdomains
    setterm --foreground default
fi
if [ $ISCLOUDWALL -eq 1 ] ; then 
    echo $DOMAIN
    setterm --foreground red
    echo "CLOUDFLARED"
    setterm --foreground default
fi

# cat FediSus.html | grep -i $DOMAIN | head -n 1 |  awk -F' ' '{print $2}'
#FEDILEVEL=`cat /home/themusicgod1/FediSus.html | grep -i $DOMAIN | head -n 1 | awk -F' ' '{print $2}'`
#echo "domain:" $DOMAIN
#echo sqlite3 $FEDIDB -line "select level from instances where name = '"$DOMAIN"';"
FEDILEVEL=`sqlite3 $FEDIDB -line "select level from instances where name = '"$DOMAIN"';" | grep -i 'level' | awk -F' ' '{print $3}'`
echo "instance level is" $FEDILEVEL
echo "max LEVEL is " $MAXLEVEL
echo "user LEVEL is " $LEVEL
#echo "why:"
echo "what:" $WHAT

# this is the line where things can go wrong if there's 2 results
# we don't want to incur another database hit.
WHYLINE=`sqlite3 $DB -line "select why from news where url = \""$WHAT"\"" | grep -i 'why'`
sqlite3 $DB -line "select why from news where url = \""$WHAT"\"" | grep -i 'why' > /tmp/whylines
WHYLINELINES=`wc -l /tmp/whylines | awk -F' ' '{print $1}'`

#WHYLINELINES=`echo $WHYLINE`
echo "WHYWLINELINES " $WHYLINELINES
if [[ $WHYLINELINES -gt 1 ]]; then
echo "we have more than one line!!!!" 
setterm --foreground red
echo "update/insert problem at $WHAT "
setterm --foreground default
exit
fi
echo $WHYLINE

echo "we are over by:"
echo "("$FEDILEVEL" - "$LEVEL")/"$MAXLEVEL
echo "("$FEDILEVEL" - "$LEVEL")/"$MAXLEVEL   | bc;
LEVEL2=`echo $LEVEL + "("$FEDILEVEL" - "$LEVEL")/"$MAXLEVEL | bc`
LEVEL3=`echo "("$FEDILEVEL" - "$LEVEL")/"$MAXLEVEL   | bc`
echo "level3" $LEVEL3
if [[ -z $LEVEL3 ]]; then
	echo "this was crashing. its zero??"
	echo "fedi level again:" $FEDILEVEL
	echo "level again:" $LEVEL
	echo "maxlevel again:" $MAXLEVEL
elif [[  $LEVEL3 -eq "0" ]] ; then
	echo "its zero"
else
	echo "id" $ID
	echo "update news set level = " $LEVEL2 " where id = "$ID";"
	echo "WHAT Is " $WHAT
fi

whoisthis $WHAT
grabSnowflake.sh $WHAT


